package servlets.games;

import hibernate.method.GameCategoryMethods;
import hibernate.method.GameMethods;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.google.gson.Gson;

/**
 * Servlet implementation class addGameCat
 */
@WebServlet("/addGameCat")
public class addGameCat extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addGameCat() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String category = request.getParameter("category");
		int category_id = Integer.parseInt(category);
		System.out.println(category_id);
		response.setCharacterEncoding("UTF-8");
        List<String> list = new ArrayList<String>();
        SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		GameCategoryMethods gcm = new GameCategoryMethods();
		list = gcm.listUnusedGameByCategory(sessionFactory, session, category_id);
		
		
        String json = null;
        

        json = new Gson().toJson(list);
        
        System.out.println(list);
        response.setContentType("application/json");
        response.getWriter().write(json);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		String category = (String) request.getParameter("category");
		String gameString = (String) request.getParameter("game");
		int category_id = Integer.parseInt(category);
		
		
			response.setContentType("text/html");

			SessionFactory sessionFactory = HibernateUtil
					.getSessionAnnotationFactory();
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			GameMethods gamem= new GameMethods();
			int game_id = gamem.nameToId(sessionFactory, session, gameString);
			GameCategoryMethods gamecat = new GameCategoryMethods();
			gamecat.addCategoryToGame(sessionFactory, session, game_id, category_id);

			try {
				session.getTransaction().commit();
				out.println("<script type=\"text/javascript\">alert(\"Dodano kategorie do gry\"); window.location.href = \"management/index.jsp?page=games&group=gamesCategories\";</script>");
			} catch (Exception e) {
				out.println("<script type=\"text/javascript\">alert(\"Nie dodano kategorii do gry\"); window.location.href = \"management/index.jsp?page=games&group=gamesCategories\";</script>");
			}

	}

}
